package com.tedu.net;

import com.sun.jndi.ldap.Ber;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

//socket通信服务器端
public class Test_Sserver {
    public static void main(String[] args) throws Exception {
        //开启服务器  --端口号为0~65535  0~1024被系统占用
        //在指定的端口号7777等待连接
        ServerSocket server = new ServerSocket(7777);
        System.out.println("服务器开启...");
        //接收客户端连接 --接收客户端的请求并建立通信
        Socket socket = server.accept();
        System.out.println("已接收到客户端的请求...");
        //开始通信
//        BufferedReader bufferein =new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        String line = "";
//        while (line != null){
//            line = bufferein.readLine();
//            System.out.println(line);
//        }
        //弊端：无法确定循环次数
        InputStream in = socket.getInputStream();
        for (int i = 0; i < 5; i++) {
            char txt = (char) in.read();
            System.out.print(txt);
        }
        OutputStream out = socket.getOutputStream();
        String tx = new Scanner(System.in).next();
        out.write(tx.getBytes());
        out.flush();
    }
}
